package com.example.poverty.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.poverty.entity.Worklog;
import com.example.poverty.service.WorklogService;
import com.example.poverty.utils.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 前端 工作日志 控制类
 */
@RestController
@RequestMapping("/worklog")
public class WorklogController {

    @Autowired
    private WorklogService worklogService;

    /**
     * 添加工作总结
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addWorklog(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String name = request.getParameter("name").trim();  // 姓名
        String post = request.getParameter("post").trim();  // 岗位
        String workPlace = request.getParameter("workPlace").trim();  //工作地点
        String workContent = request.getParameter("workContent").trim();  //工作内容
        String completeSchedule = request.getParameter("completeSchedule").trim();  //完成进度
        String workSummary = request.getParameter("workSummary").trim();  //工作总结
        String remark = request.getParameter("remark").trim();  //备注
        String time = request.getParameter("time").trim();  //时间

        // 把时间转成Date格式
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date timeDate = new Date();
        try {
            timeDate = dateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Worklog worklog = new Worklog();
        worklog.setName(name);
        worklog.setPost(new Byte(post));
        worklog.setWorkPlace(workPlace);
        worklog.setWorkContent(workContent);
        worklog.setCompleteSchedule(completeSchedule);
        worklog.setWorkSummary(workSummary);
        worklog.setRemark(remark);
        worklog.setTime(timeDate);
        boolean flag = worklogService.insert(worklog);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "添加成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "添加失败");
        return jsonObject;
    }

    /**
     * 修改工作总结
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateWorklog(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String id = request.getParameter("id").trim();  //主键
        String name = request.getParameter("name").trim();  // 姓名
        String post = request.getParameter("post").trim();  // 岗位
        String workPlace = request.getParameter("workPlace").trim();  //工作地点
        String workContent = request.getParameter("workContent").trim();  //工作内容
        String completeSchedule = request.getParameter("completeSchedule").trim();  //完成进度
        String workSummary = request.getParameter("workSummary").trim();  //工作总结
        String remark = request.getParameter("remark").trim();  //备注
        String time = request.getParameter("time").trim();  //时间

        // 把时间转成Date格式
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date timeDate = new Date();
        try {
            timeDate = dateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Worklog worklog = new Worklog();
        worklog.setId(Integer.parseInt(id));
        worklog.setName(name);
        worklog.setPost(new Byte(post));
        worklog.setWorkPlace(workPlace);
        worklog.setWorkContent(workContent);
        worklog.setCompleteSchedule(completeSchedule);
        worklog.setWorkSummary(workSummary);
        worklog.setRemark(remark);
        worklog.setTime(timeDate);
        boolean flag = worklogService.update(worklog);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "修改成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "修改失败");
        return jsonObject;
    }

    /**
     * 删除工作总结
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Object deleteWorklog(HttpServletRequest request) {
        String id = request.getParameter("id").trim();  //主键
        boolean flag = worklogService.delete(Integer.parseInt(id));
        return flag;
    }

    /**
     * 根据主键查询工作总结
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.GET)
    public Object selectByPrimaryKey(HttpServletRequest request) {
        String id = request.getParameter("id").trim();  //主键
        return worklogService.selectByPrimaryKey(Integer.parseInt(id));
    }

    /**
     * 查询所有的工作总结
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/allWorklog", method = RequestMethod.GET)
    public Object allWorklog(HttpServletRequest request) {
        return worklogService.allWorklog();
    }

    /**
     * 根据姓名查询工作总结
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getByName", method = RequestMethod.GET)
    public Object getByName(HttpServletRequest request) {
        String name = request.getParameter("name").trim();
        return worklogService.getByName(name);
    }
}
